<?php
$phpVersion = phpversion();

function CurrentPageURL() {
	$pageURL = 'http://';
	$pageURL .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
	return $pageURL;
	}

function greaterDate($start_date,$end_date)
{
  $start = new Datetime($start_date);
  $end = new Datetime($end_date);
  if ($start > $end)
   return 1;
  else
   return 0;
}

$color = "#eeeeee";
$color1 = "#e0e0e0";
$color2 = "#eeeeee";

function relocate($referer) {
	// determine if referrer has any msg=X or id=X variables attached and remove
	if (strstr($referer,"&msg")) { 
	$pattern = array("/[0-9]/", "/&msg=/");
	$referer = preg_replace($pattern, "", $referer);
	}
	return $referer;
}


// ---------------------------- Date Conversion -----------------------------------------
// http://www.phpbuilder.com/annotate/message.php3?id=1031006
function dateconvert($date,$func) {
if ($func == 1)	{ //insert conversion
list($day, $month, $year) = split('[/.-]', $date); 
$date = "$year-$month-$day"; 
return $date;
	}
if ($func == 2)	{ //output conversion
list($year, $month, $day) = split('[-.]', $date); 
$date = "$day, $year"; 
if ($month == "01" ) { echo "January "; }
if ($month == "02" ) { echo "February "; }
if ($month == "03" ) { echo "March "; }
if ($month == "04" ) { echo "April "; }
if ($month == "05" ) { echo "May "; }
if ($month == "06" ) { echo "June "; }
if ($month == "07" ) { echo "July "; }
if ($month == "08" ) { echo "August "; }
if ($month == "09" ) { echo "September "; }
if ($month == "10" ) { echo "October "; }
if ($month == "11" ) { echo "November "; }
if ($month == "12" ) { echo "December "; }
return $date;
	}
if ($func == 3)	{ //output conversion
list($year, $month, $day) = split('[-.]', $date); 
$date = "$day, $year"; 
if ($month == "01" ) { echo "Jan "; }
if ($month == "02" ) { echo "Feb "; }
if ($month == "03" ) { echo "Mar "; }
if ($month == "04" ) { echo "Apr "; }
if ($month == "05" ) { echo "May "; }
if ($month == "06" ) { echo "Jun "; }
if ($month == "07" ) { echo "Jul "; }
if ($month == "08" ) { echo "Aug "; }
if ($month == "09" ) { echo "Sep "; }
if ($month == "10" ) { echo "Oct "; }
if ($month == "11" ) { echo "Nove "; }
if ($month == "12" ) { echo "Dec "; }
return $date;
	}
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

function get_material_type($type) {
	switch($type) {
		case "1": $return = "Textbook"; break;
		case "2": $return = "Journal/Periodical"; break;
		case "1": $return = "Web"; break;
		case "1": $return = "Textbook"; break;
		case "1": $return = "Textbook"; break;
		case "1": $return = "Textbook"; break;
		case "1": $return = "Textbook"; break;
		
	}
	return $return;
}

function truncate_string ($str, $length, $trailing) { 
      // take off chars for the trailing 
      $length-=strlen($trailing);
	  // string exceeded length, truncate and add trailing characters
      if (strlen($str) > $length) return substr($str,0,$length).$trailing; 
      // string was already short enough, return the string 
      else  $res = $str; 
      return $res; 
}


function get_material_info($id,$type,$method) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_materials = "SELECT * FROM materials WHERE id='$id'"; 
	$materials = mysql_query($query_materials, $connection) or die(mysql_error());
	$row_materials = mysql_fetch_assoc($materials);
	if ($type != "default") $type = $row_materials['matType']; else $type = $type;
	
	switch($type) {
		// Book
		case "1": 
			if ($method == "1") 	{ 
				$return = $row_materials['matAuthor']." (".$row_materials['matYear']."). <em>".$row_materials['matTitle']."</em>. ".$row_materials['matCity'].": ".$row_materials['matPublisher']."."; if ($row_materials['matISBN'] != "") $return .= " ISBN: <a href='http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias&amp;field-keywords=".$row_materials['matISBN']."' target='_blank' title='Search Amazon for this ISBN'>".$row_materials['matISBN']."</a>."; 
			}
			if ($method == "2")		$return = "Textbook";
		break;
		
		// Periodical
		case "2": 
			if ($method == "1")		$return =  $row_materials['matAuthor']." (".$row_materials['matYear']."). ".$row_materials['matTitle'].". <em>".$row_materials['matPublisher'].", ".$row_materials['matVolume']."</em> (".$row_materials['matIssue']."), ".$row_materials['matPages'].".";
			if ($method == "2")		$return = "Journal/Periodical";
		break;
		
		// Website
		case "3": 
		if ($method == "1")		$return = $row_materials['matAuthor']." (".$row_materials['matYear']."). <em>".$row_materials['matTitle']."</em>. Retrieved from: <a href='".$row_materials['matURL']."' target='_blank'>".truncate_string($row_materials['matURL'],50,'...')."</a>";
		if ($method == "2")		$return = "Web";
		break;
		
		// Dissertation
		case "4": 
		$return = $row_materials['matAuthor']." (".$row_materials['matYear']."). <em>".$row_materials['matTitle']."</em> (Doctoral dissertation). Retrieved from: ".$row_materials['matEglobal'].". (Accession No. ".$row_materials['matISBN'].").";
		if ($method == "2")		$return = "Dissertation";
		break;
		
		// Chapter in a Book
		case "5": 
		if ($method == "1")		$return = "";
		if ($method == "2")		$return = "Book Chapter";
		break;
		
		// Other
		case "6": 
		if ($method == "1")		$return = "";
		if ($method == "2")		$return = "Other";
		break;
		
		case "default": $return = $row_materials['matTitle'];
		break;
		
	}
	return $return;
	
}

function view_progress($cid,$mid,$section) {
	if (strstr($section,"step")) $section = "step"; else $section = $section;
	if (strstr($section,"module")) $section = "module"; else $section = $section;
	switch ($section) {
		case "step":
			$section_output = "view_proposal";
			$title = "View Proposal Progress";
		break;
		
		case "module":
			$section_output = "view_module";
			$title = "View Module Progress";
		break;
		
		default:
			$section_output = "view_course";
			$title = "View Course Progress";
		break;
	}
	
	$return = '<span class="icon"><img src="images/tick.png" border="0" align="absmiddle" /></span><a href="index.php?section='.$section_output.'&amp;action='.$action.'&amp;cid='.$cid.'&amp;modal_window=true" title="'.$title.'" id="modal_window_link">'.$title.'</a>';
	
	return $return;
}

function get_latest_version($dbTable,$cid) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_version = "SELECT * FROM $dbTable WHERE cid='$cid' ORDER BY vid DESC LIMIT 1"; 
	$version = mysql_query($query_version, $connection) or die(mysql_error());
	$row_version = mysql_fetch_assoc($version);
	return $row_version['id']."*".$row_version['vid'];
}

function previous_versions($dbTable,$cid,$latest_vid,$modal_window,$section,$action) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_prev_versions = sprintf("SELECT * FROM $dbTable WHERE cid='%s' AND vid < '%s'", $cid, $latest_vid);
	$prev_versions = mysql_query($query_prev_versions, $connection) or die(mysql_error());
	$row_prev_versions = mysql_fetch_assoc($prev_versions);
	$totalRows_prev_versions = mysql_num_rows($prev_versions);
		//echo $query_cd_loop;
		//echo $totalRows_cd_loop;
	if (($totalRows_prev_versions > 0) && ($modal_window == "false")) { 
			$return = "<div class='stacked'>View: ";
				do 	{ 
					$return .= "<a id='modal_window_link' href='index.php?section=".$section."&amp;action=".$action."&amp;go=default&amp;view=default&amp;dbTable=".$dbTable."&cid=".$cid."&id=".$row_prev_versions['id']."&amp;modal_window=true' title='Course Description (Version ".$row_cd_loop['vid'].")'>".$row_prev_versions['vid']."</a>&nbsp;&nbsp;"; 
					} while ($row_prev_versions = mysql_fetch_assoc($prev_versions)); 
			$return .= "</div>";
			} else $return = "None";
	return $return;
}

function changes_needed($changes) {
	if ($changes == "Y") $return = "<div class='stacked'><span class='icon'><img src='images/error.png' align='absmiddle' border='0'></span>Yes</div>";
	else $return = "<div class='stacked'><span class='icon'><img src='images/tick.png' align='absmiddle' border='0'></span>No</div>";	
	return $return;
}

function view_comments($dbTable,$section,$cid,$id,$view,$comment_count,$view_type) {
	if ($comment_count > 0) {
	$return = "<div class='stacked'><span class='icon'><img src='images/comment.png' align='absmiddle' border='0'></span>Yes - <a ";
	if ($view_type = "modal") $return .= "id='modal_window_link' ";
	$return .= "href='index.php?section=".$section."&amp;go=default&amp;view=".$view."&amp;dbTable=".$dbTable."&amp;cid=".$cid."&amp;id=".$id;
	if ($view_type = "modal") $return .= "&amp;modal_window=true";
	
	$return .= "'";
	
	$return .= " title='View ID Comments'>View</a></div>";
	}
	else $return = "<div class='stacked'><span class='icon'><img src='images/comment_fade.png' align='absmiddle' border='0'></span>No</div>";
	return $return;
}

function nav_view_proposal($cid) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	
	$table = array("course_description","course_project","learning_outcomes","materials","project_deliverable");
	
	foreach ($table as $dbTable) {
		$query_version = "SELECT COUNT(*) as count FROM $dbTable WHERE cid='$cid'"; 
		$version = mysql_query($query_version, $connection) or die(mysql_error());
		$row_version = mysql_fetch_assoc($version);
		$total[] = $row_version['count'];
	}
	$total_records = array_sum($total);
	//return $total_records;
	if ($total_records > 0) return true;
	else return false;
}

function display_content($id,$dbTable) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_display_content = sprintf("SELECT * FROM $dbTable WHERE id='%s'", $id);
	$display_content = mysql_query($query_display_content, $connection) or die(mysql_error());
	$row_display_content = mysql_fetch_assoc($display_content);
	
	if ($dbTable == "themes") {
		if ($row_display_content['theme_text'] != "") $return = "<a id='modal_window_link'  href='index.php?section=view_course&amp;action=review&amp;go=theme&amp;cid=".$row_display_content['cid']."&amp;mid=".$row_display_content['mid']."&amp;id=".$row_display_content['id']."&amp;modal_window=true' title='View ".$row_display_content['theme_title']."'>".$row_display_content['theme_title']."</a>";
		else $return = $row_display_content['theme_title'];
	}
	
	if ($dbTable == "assignments") {
		if (($row_display_content['assign_overview'] != "") && ($row_display_content['assign_procedure'] != "")) $return = "<a id='modal_window_link'  href='index.php?section=view_course&amp;action=review&amp;go=assignment&amp;cid=".$row_display_content['cid']."&amp;mid=".$row_display_content['mid']."&amp;id=".$row_display_content['id']."&amp;modal_window=true' title='View ".$row_display_content['assign_title']."'>".$row_display_content['assign_title']."</a>";
		else $return = $row_display_content['assign_title'];
	}
	
	return $return;
	
}

function lo_check($cid,$del_num) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_lo = sprintf("SELECT COUNT(*) as 'count' FROM learning_outcomes WHERE cid='%s' AND del_num='%s' ORDER BY id", $cid, $del_num);
	$lo = mysql_query($query_lo, $connection) or die(mysql_error());
	$row_lo = mysql_fetch_assoc($lo);
	if ($row_lo['count'] > 0) return TRUE; else return FALSE;
}

function lo_deliverable_match($cid) {
	require (CONFIG.'config.php');
	mysql_select_db($database, $connection);
	$query_pd = sprintf("SELECT del_num FROM project_deliverable WHERE cid='%s'", $cid);
	$pd = mysql_query($query_pd, $connection) or die(mysql_error());
	$row_pd = mysql_fetch_assoc($pd);
	$totalRows_pd = mysql_num_rows($pd);
	
	do { $a = $row_pd['del_num']; } while ($row_pd = mysql_fetch_assoc($pd));
	
	foreach ($a as $del_num) {
		$query_lo = sprintf("SELECT COUNT(*) as 'count' FROM learning_outcomes WHERE cid='%s' AND del_num='%s' ORDER BY id", $cid, $del_num);
		$lo = mysql_query($query_lo, $connection) or die(mysql_error());
		$row_lo = mysql_fetch_assoc($lo);
		$b[] = $row_lo['count'];
	}
	$c = (array_sum($b));
	if ($c == $totalRows_pd) return TRUE; 
	else return FALSE;
}


// -------------------------- Navigation Functions ---------------------------------------

function top_link ($section,$modal_window) {
	if (($section == "view_proposal") && ($modal_window == "false")) $return = '<p style="margin-top: 1.25em;"><a href="#top">Top</a></p>'; 
	elseif (($section == "view_course") && ($modal_window == "false")) $return = '<p style="margin-top: 1.25em;"><a href="#top">Top</a></p>'; 
	else $return = '';
	return $return;
}

function previous_link($section,$action,$go,$modal_window) {
	$previous_link = $_SERVER['HTTP_REFERER'];
	//if (strstr($previous_link,'&modal_window=true')) { 
		if (($action != "view") && (($view != "proposal_detail") && ($go != "missing"))) $return = "<div class='stacked'><span class='icon'><img src='images/arrow_left.png' align='absmiddle' border='0'></span><a href='".$previous_link."'>Back to Previous</a></div>";
	//}
	//else $return = '';
	return $return;
}




?>